package com.mooc.swxy.control;

import com.mooc.swxy.pojo.CourseVt;
import com.mooc.swxy.pojo.UserInfo;
import com.mooc.swxy.system.ReqURL;
import com.mooc.swxy.system.ResponseMap;
import com.mooc.swxy.task.WkService;
import com.mooc.swxy.task.WkTask;
import com.mooc.swxy.tool.RequestTool;
import com.mooc.swxy.tool.UserTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Siaze
 * @date 2022/4/19
 * 获取课程信息
 */
@RestController
public class Course {

    private static final Logger logger = LoggerFactory.getLogger(Course.class);

    @Autowired
    private RequestTool requestTool;


    @GetMapping("/course")
    public ResponseMap course(@CookieValue("token") String token) {
        Map<String, String> params = new LinkedHashMap<>();
        params.put("platform", "Android");
        params.put("token", token);
        params.put("version", "1.4.6");
        String res = requestTool.postRequest(ReqURL.BASE_URL.concat(ReqURL.COURSE_URL), MediaType.MULTIPART_FORM_DATA, params , token, String.class);
        CourseVt courseVt = JSON.parseObject(res, CourseVt.class);
        return new ResponseMap(200,courseVt.getMsg()).putData("list", courseVt.getResult().getList());
    }

    @PostMapping("/start")
    public ResponseMap start(@CookieValue("token") String token, @RequestBody Map<String,String> params) {
        String sign = params.get("sign");
        boolean pass = UserTool.startTask(sign);
        if(!pass){
            return new ResponseMap(400, "通行证错误");
        }
        params.put("platform", "Android");
        params.put("token", token);
        params.put("version", "1.4.6");
        logger.info("params: {}", params);
        return new WkTask(new RequestTool()).chapter(params);
    }

    @GetMapping("/getMyProcess")
    public ResponseMap getProcess(@CookieValue("token") String token){
        UserInfo userInfo = UserTool.getInfo(token);
            return new ResponseMap(200, "获取成功").putData("list" ,WkService.getProcess(userInfo.getResult().getData().getNumber())).putData("userInfo", userInfo.getResult().getData());
    }


    @GetMapping("/getAllProcess")
    public ResponseMap getAllProcess(){
        return new ResponseMap(200, "获取成功").putData("list" ,WkService.getAllProcess());
    }
}
